﻿using System.Text;
using UnityEngine;
public class ShowFrame : MonoBehaviour
{
#if LTDEBUG
    //GUI显示的帧率
    private float m_FrameCount = 0;

    //一个m_ProcessInterval时长单元下逻辑更新帧数累计
    private float m_FrameCountTikc = 0;

    //一个m_ProcessInterval时长单元下逻辑更新时长累计
    private float m_LogicUpdateTimeTick = 0;

    //帧率计算间隔时长
    private float m_ProcessInterval = 0.1f;

    //真实时间流逝时长(不受时间缩放大小影响)
    private float m_TimeTick = 0;

    private void FixedUpdate()
    {
        m_TimeTick += Time.fixedUnscaledDeltaTime;
    }

    private StringBuilder m_Sb = new StringBuilder();

    // Update is called once per frame
    private void Update()
    {
        m_LogicUpdateTimeTick += Time.deltaTime;
        m_FrameCountTikc += 1;
        if (m_TimeTick >= m_ProcessInterval)
        {
            m_FrameCount = m_FrameCountTikc / m_LogicUpdateTimeTick;
            m_TimeTick = 0;
            m_FrameCountTikc = 0;
            m_LogicUpdateTimeTick = 0;
            m_Sb.Clear();
            m_Sb.Append("FPS:" + m_FrameCount.ToString("f0"));
        }
    }

    private void OnGUI()
    {
        GUIStyle s = GUI.skin.label;
        s.fontSize = 24;
        s.fontStyle = FontStyle.Bold;
        s.alignment = TextAnchor.UpperLeft;
        Color color = Color.green;
        if (m_FrameCount < 120f)
        {
            color = Color.red;
        }
        s.normal.textColor = color;
        GUILayout.TextArea(m_Sb.ToString(), s, GUILayout.Width(200), GUILayout.Height(60));
    }
#endif
}
